package com.example.jpa.entity;

import com.example.jpa.enums.SexEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Programmer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore     //返回JSON到前端，不包含该属性
    private Long id;

    @Column(length = 64,nullable = false,unique = true)
    private String name;

    @Enumerated(EnumType.STRING)
    private SexEnum sex;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")
    //返回JSON到前端，如何格式化日期
    private Date birthday;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonIgnore
    @Transient
    private Date startDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonIgnore
    @Transient //短暂，不持久,该属性不会在数据库创建字段
    private Date endDate;

    @OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name = "boy_friend_id")
    private BoyFriend boyFriend;

    //CascadeType级联删除
    @OneToMany(mappedBy = "programmer",fetch = FetchType.LAZY,cascade = CascadeType.ALL) //通过mappedBy表示外键在Computer实体的哪个属性上
    @JsonIgnoreProperties("programmer") //排除Computer中的programmer属性，防止无限嵌套查询
    private List<Computer> computers;

    @ManyToMany(mappedBy = "programmers",fetch = FetchType.LAZY,cascade = CascadeType.ALL)//懒加载
    @JsonIgnoreProperties("programmers")
    private List<Project> projects;
}
